package controllers

import (
	"github.com/beego/beego/v2/server/web"
)

type BaseController struct {
	web.Controller
}

func (c *BaseController) Prepare() {
	if c.Ctx.Input.URL() == "/login" {
		return
	}

	user := c.GetSession("user")
	if user == nil {
		c.Redirect("/login", 302)
		return
	}

	c.Data["UserName"] = user
}

func (c *BaseController) JSONResponse(success bool, message string, data ...interface{}) {
	response := map[string]interface{}{
		"success": success,
		"message": message,
	}

	if len(data) > 0 {
		response["data"] = data[0]
	}

	c.Data["json"] = response
	c.ServeJSON()
}
